package Scene4;
import Picture.GameImage;
import Scnen3.Scene3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

public class AnimalPanel extends JPanel {
    boolean isJump = false;
    Grass grass = new Grass();
    public int score = 0;
    Scene3 scene3 = new Scene3();
    BufferedImage[] imgs = new BufferedImage[11];


    Scene4 scene = new Scene4();
    public wolf w = new wolf();
    public int flag = 0;
    public int flag1 = 0;
    public int flag2 = 0;
    public Cow cow = new Cow();
    public void resize(JFrame frame){
        frame.setSize(1536,1110);
    }



    public AnimalPanel(JButton button) {
        new Thread(()->{

                while(true) {
                    if(lose==1||win==1){
                        this.add(button);
                    }
                    cow.changeImage();
                    if(begin==1)
                    w.move();
                    w.changeImage();
                    repaint();
                    if(score == 10){
                        score = 0;
                        end = 1;
                        begin = 0;
                        win = 1;
                    }
                    try {
                        Thread.sleep(150);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
        }).start();
        for (int i = 0; i < 10; i++) {
            imgs[i] = GameImage.get("/Picture/数字/"+i+".png");
        }
        this.setForeground(Color.RED);
        new Thread(() -> {
            while (true){
                cow.jump();
                repaint();
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        this.requestFocus();
        KeyAdapter adapter = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int code = e.getKeyCode();
                switch (code){
                    case KeyEvent.VK_W:
                        if(cow.y>=cow.groundY){
                            isJump = false;
                        }
                        if(isJump == false) {
                            changeY();
                        }
                        break;

                    case KeyEvent.VK_A:
                        cow.dir = 1;
                        cow.x-=20;
                        break;
                    case KeyEvent.VK_D:
                        cow.dir = 2;
                        cow.x+=20;
                        break;
                }
                repaint();

            }
        };
        this.addKeyListener(adapter);
        setBackground(Color.BLACK);

    }

    private void changeY() {
        cow.speed = 120;
        isJump = true;
    }

    public void paint(Graphics g){
        if(Math.abs(cow.x-grass.x)<=30&&cow.y<=cow.groundY+50){
                    grass.reAppear();
                    score+=1;
        }
        super.paint(g);
        if((Math.abs(cow.y-w.y)==100&&Math.abs(cow.x-w.x)<=40)||
                (Math.abs(cow.y-w.y)<=120&&Math.abs(cow.x-w.x)<=40)){
            flag2 = 1;
            if(end==0) {
                lose = 1;
                end = 1;
            }

        }
        if(begin == 0&&end==0) {
            g.drawImage(scene.map.get("bg"), 0, 0, null);
            g.drawImage(scene3.map.get("提示框"), 500, 0, null);
            g.drawImage(scene3.map.get("11"),720,250,null);
            g.drawImage(scene3.map.get("21"),580,350,null);
            g.drawImage(scene3.map.get("31"),740,550,null);
            g.drawImage(scene3.map.get("41"),650,650,null);
            g.drawImage(scene3.map.get("log"),600,200,null);
            g.drawImage(scene3.map.get("log"),600,500,null);
            g.drawImage(scene3.map.get("button"),700,830,null);
            g.drawImage(scene3.map.get("begin"),710,840,null);
            g.drawImage(scene3.map.get("dec1"),500,150,null);
            g.drawImage(scene3.map.get("dec2"),500,200,null);
            g.drawImage(scene3.map.get("dec1"),500,300,null);
            g.drawImage(scene3.map.get("dec2"),500,400,null);
            g.drawImage(scene3.map.get("dec1"),500,450,null);
            g.drawImage(scene3.map.get("dec2"),500,600,null);
            g.drawImage(scene3.map.get("dec1"),500,600,null);
            g.drawImage(scene3.map.get("dec1"),500,750,null);
            g.drawImage(scene3.map.get("dec2"),500,800,null);


            g.drawImage(scene3.map.get("dec1-"),1080,150,null);
            g.drawImage(scene3.map.get("dec2-"),1080,200,null);
            g.drawImage(scene3.map.get("dec1-"),1080,300,null);
            g.drawImage(scene3.map.get("dec2-"),1080,400,null);
            g.drawImage(scene3.map.get("dec1-"),1080,450,null);
            g.drawImage(scene3.map.get("dec2-"),1080,600,null);
            g.drawImage(scene3.map.get("dec1-"),1080,600,null);
            g.drawImage(scene3.map.get("dec1-"),1080,750,null);
            g.drawImage(scene3.map.get("dec2-"),1080,800,null);
        }
        if(flag2==0&&begin ==1&&end == 0) {
            g.drawImage(scene.map.get("bg"), 0, 0, null);
            g.drawImage(w.img, w.x, w.y, null);
            g.drawImage(cow.img, cow.x, cow.y, null);
            g.drawImage(grass.img,grass.x,grass.y,null);
            g.drawImage(scene.map.get("计分板"),400,50,null);
            g.drawImage(grass.img,620,110,null);
            g.drawImage(imgs[score],750,70,null);

        }
        if(end==1){
            g.drawImage(scene.map.get("bg"), 0, 0, null);
            if(win == 1) {
                g.drawImage(GameImage.get("/Picture/win.png"), 400, 300, null);
                g.drawImage(GameImage.get("/Picture/数字/win.png"), 550, 650, null);
                g.drawImage(GameImage.get("/Picture/Scene3/button.png"), 650, 750, null);
                g.drawImage(GameImage.get("/Picture/数字/好耶.png"), 680, 770, null);
            }
            if(lose==1) {
                g.drawImage(GameImage.get("/Picture/lose.png"), 400, 300, null);
                g.drawImage(GameImage.get("/Picture/数字/lose.png"), 550, 650, null);
                g.drawImage(GameImage.get("/Picture/Scene3/button.png"), 650, 750, null);
                g.drawImage(GameImage.get("/Picture/数字/再来一遍.png"), 660, 760, null);
            }
        }



    }
    public int begin = 0;
    public int end = 0;
    public int win = 0;
    public int lose = 0;




}

